use strict;
use Stdlog;
use File::Spec;
use Test::More tests => 13;

# Test 1 -- use Stdlog::Writer::File
BEGIN { use_ok('Stdlog::Writer::File');}


# Test 2 -- setup temp dir and ensure test temp files are not there
my $log_dir = File::Spec->tmpdir();
unlink("$log_dir/test-Foo.log");
unlink("$log_dir/test-Bar.log");
ok(-e $log_dir && -d $log_dir, "Temp dir exists");

# Test 3 -- create STDERR writer
my $writer = Stdlog::Writer->new({
    writer => 'File',
    filename => "$log_dir/test-%{package}.log"
});
ok($writer && ref($writer) eq 'Stdlog::Writer::File',
    "Created Stdlog::Writer::File");

# Test 4 -- check file is not yet open
ok(!defined $writer->{fh},
    "File is not yet open");

# Test 5-7 -- check file open
$writer->open_file({package => 'Foo'});
is($writer->{fname}, "$log_dir/test-Foo.log",
    "File name is correct");
ok($writer->{fh},
    "File is open");
ok(-e "$log_dir/test-Foo.log",
    "File exists");

# Test 8-10 -- check that file is the same
my $fh = $writer->{fh};
$writer->open_file({package => 'Foo'});
is($writer->{fname}, "$log_dir/test-Foo.log",
    "File name is still correct");
ok(defined $writer->{fh} && $writer->{fh} == $fh,
    "File is open and the same");
ok( -e "$log_dir/test-Foo.log",
    "Same file still exists");

# Test 11-13 -- check that file is different
$fh = $writer->{fh};
$writer->open_file({package => 'Bar'});
is($writer->{fname}, "$log_dir/test-Bar.log",
    "File name is different");
isnt($writer->{fh}, $fh,
    "File is open and different");
ok( -e "$log_dir/test-Bar.log",
    "Different file exists");

# cleanup
unlink("$log_dir/test-Foo.log");
unlink("$log_dir/test-Bar.log");

